﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Dyao.Book.BusApplication;
using Dyao.Book.Core;
using Dyao.Bus.Extensions.Configuration;
using Foundation.Core;
using Foundation.EntityFrameworkCore;
using Foundation.Redis;
using Foundation.Service.Bus.Core;
using Foundation.Service.Bus.Host;
using Foundation.Service.Bus.RabbitMQ;
using Foundation.Spider;
using Foundation.Spider.Scheduler;
using Foundation.Spider.Scheduler.Component;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Dyao.Book.Bus.Long
{
    public class Program
    {
        private static void AddBusService(IServiceCollection services)
        {
            // Register all of your bus services as singleton
            services.AddSingleton<IBusService, SpiderLongRunningBusService>();
        }

        static void Main(string[] args)
        {
            var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

            var builder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .UseQueueDistinctBfsScheduler<HashSetDuplicateRemover>()
                .ConfigureServices((context, services) =>
                    {
                        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

                        services.AddLogging(loggerBuilder =>
                        {
                            loggerBuilder.AddLog4Net();
                        });

                        services.AddSingleton<ICurrentUser, CurrentUser>(); // 用户

                        services.AddDbContext(configuration);

                        services.AddCustomCaching(configuration); // 缓存

                        services.AddObjectMapping();

                        AddBusService(services);

                        services.AddServiceBus(configuration, busConfig =>
                        {
                            busConfig.UseServiceBusHost(services, host =>
                            {
                                host.UseRabbitMQ(services);
                            });

                            busConfig.UsePublisher(services, configuration, publisher =>
                            {
                                publisher.UseRabbitMQ(services);
                            });
                        });

                        services.AddSpider(spiderOptions =>
                        {
                            spiderOptions.Speed = 1;
                            spiderOptions.Depth = 1;
                            spiderOptions.EmptySleepTime = 5;
                        });

                        services.AddReplacement(configuration);
                        services.AddEntityFrameworkCoreReplacement(configuration);

                        services.AddRedisOperation(configuration);
                        services.AddIdCreate();

                    }
                 )
                .ConfigureContainer<ContainerBuilder>((context, containerBuilder) =>
                {
                    containerBuilder.AddServices();
                });


            //Start the host.
            builder.Build().Setup().Run();

        }
    }
}
